package com.dingreading.cloud.admin.controller;

import com.dingreading.cloud.admin.dto.StoreProjectCycleDto;
import com.dingreading.cloud.admin.entity.StoreProjectCycle;
import com.dingreading.cloud.admin.entity.StoreProjects;
import com.dingreading.cloud.admin.service.StoreProjectCycleService;
import com.dingreading.cloud.admin.service.StoreProjectsService;
import com.dingreading.cloud.admin.util.ProjectCycleUtil;
import com.dingreading.cloud.admin.util.bean.CmorderUtil;
import com.dingreading.cloud.admin.util.redis.RedisService;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.DescribeException;
import com.dingreading.cloud.common.util.PageBeanDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.common.util.RedisParam;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/system/storeProjectCycle/v2")
@Api(tags = "总部-门店服务周期管理V2")
public class StoreProjectCycleControllerV2 {

    @Resource
    protected StoreProjectCycleService cycleService;
    @Resource
    protected StoreProjectsService projectsService;
    @Resource
    protected RedisService redisService;

    @ApiOperation(value = "默认服务分页列表")
    @PostMapping(value = "/pageList", produces = "application/json; charset=UTF-8")
    public R<Object> pageList(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "周期名称") @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(value = "是否禁用 1.启用 0.禁用") @RequestParam(value = "enabled", required = false) Integer enabled
    ) {
        Page<StoreProjectCycle> page = cycleService.pageList(pageUtil, keyword, enabled);

        PageBeanDto data = new PageBeanDto();
        data.setTotal(page.getTotalRow());
        data.setList(page.getRecords());
        return R.ok(data);
    }

    @Transactional(rollbackFor = DescribeException.class)
    @ApiOperation(value = "新增/修改")
    @PostMapping(value = "/newBean", produces = "application/json; charset=UTF-8")
    public R<Object> newBean(
            @ApiParam(value = "员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "uid") @RequestParam(value = "uid", required = false) String uid,
            @ApiParam(value = "服务uid") @RequestParam(value = "projectUid") String projectUid,
            @ApiParam(value = "周期名称") @RequestParam(value = "cycleName") String cycleName,
            @ApiParam(value = "活动天数") @RequestParam(value = "days") Integer days,
            @ApiParam(value = "活动次数") @RequestParam(value = "lessons", required = false) Integer lessons,
            @ApiParam(value = "总价") @RequestParam(value = "totalAmount") Double totalAmount,
            @ApiParam(value = "单价") @RequestParam(value = "unitAmount") Double unitAmount
    ) throws Exception {
        if (StringUtils.isBlank(cycleName))
            return R.fail("周期名称不能为空");
        if (null == days || days <= 0)
            return R.fail("请填写正确的活动天数");
        if (totalAmount == null || totalAmount < 0)
            return R.fail("请填写正确的总价");
        if (unitAmount == null || unitAmount < 0)
            return R.fail("请填写正确的单价");

        StoreProjects projects = projectsService.getByUid(projectUid);
        if (null == projects)
            return R.fail("服务获取失败");
        if (CmorderUtil.isConsumeLesson(projects.getLessonStatus(), projects.getFreeStatus()))
            if (lessons == null || lessons <= 0)
                return R.fail("请填写正确的活动次数");

        boolean b = cycleService.haveName(uid, projectUid, cycleName);
        if (b) return R.fail("周期名称不能重复");

        StoreProjectCycle data = null;
        if (StringUtils.isBlank(uid)) {
            // 获取最大的序号
            int maxOrderNum = cycleService.getMaxShowOrder(projectUid) + 1;
            // 新增
            data = ProjectCycleUtil.newCycle(
                    null, projectUid, cycleName, days, lessons, totalAmount, unitAmount, maxOrderNum, 1,
                    headStaffUid, headStaffName
            );
            cycleService.saveOrUpdate(data);
        } else {
            // 修改
            boolean b1 = cycleService.updateInfo(
                    uid, cycleName, days, lessons, totalAmount, unitAmount, headStaffUid, headStaffName
            );
            if (!b1)
                return R.fail("更新失败");
        }

        // 清除门店服务周期缓存数据
        redisService.setNullLike(RedisParam.PROJECT_CYCLE);

        return R.ok();
    }

    @ApiOperation(value = "获取详情")
    @PostMapping(value = "/getBean", produces = "application/json; charset=UTF-8")
    public R<Object> getBean(
            @ApiParam(value = "员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "uid") @RequestParam(value = "uid") String uid
    ) {
        StoreProjectCycle data = cycleService.getByUid(uid);
        if (data == null) return R.fail("获取失败");

        return R.ok(data);
    }

    @ApiOperation(value = "更新启用状态")
    @PostMapping(value = "/updateEnabled", produces = "application/json; charset=UTF-8")
    public R<Object> updateEnabled(
            @ApiParam(value = "员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "uid") @RequestParam(value = "uid") String uid,
            @ApiParam(value = "状态 0.禁用 1.启用") @RequestParam(value = "enabled") Integer enabled
    ) {
        boolean l = cycleService.updateEnabled(uid, enabled, headStaffUid, headStaffName);
        if (!l) return R.fail("更新失败");

        // 清除门店服务周期缓存数据
        redisService.setNullLike(RedisParam.PROJECT_CYCLE);

        return R.ok();
    }

    @ApiOperation(value = "删除")
    @PostMapping(value = "/dropBean", produces = "application/json; charset=UTF-8")
    public R<Object> dropBean(
            @ApiParam(value = "员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "uid") @RequestParam(value = "uid") String uid
    ) {
        long l = cycleService.dropBean(uid);
        if (l <= 0)
            return R.failDel();

        // 清除门店服务周期缓存数据
        redisService.setNullLike(RedisParam.PROJECT_CYCLE);

        return R.ok();
    }

    @ApiOperation(value = "上移/下移")
    @PostMapping(value = "/upOrDown", produces = "application/json; charset=UTF-8")
    public R<Object> upOrDown(
            @ApiParam(value = "员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "uid") @RequestParam(value = "uid") String uid,
            @ApiParam(value = "类型 1.上移 2.下移") @RequestParam(value = "type") Integer type
    ) {
        StoreProjectCycle data = cycleService.getByUid(uid);
        if (data == null) return R.fail("获取失败");

        if (type.equals(1)) {
            // 上移
            StoreProjectCycle preData = cycleService.getLtOrderNum(data.getProjectUid(), data.getOrderNum());
            if (preData == null) return R.fail("已在最前");
            // 和前一条记录的序号互换
            Integer prevOrderNum = preData.getOrderNum();
            Integer curOrderNum = data.getOrderNum();
            // 序号互换
            cycleService.updateOrderNum(preData.getUid(), curOrderNum, headStaffUid, headStaffName);
            cycleService.updateOrderNum(uid, prevOrderNum, headStaffUid, headStaffName);
        } else if (type.equals(2)) {
            // 下移
            StoreProjectCycle backData = cycleService.getGtOrderNum(data.getProjectUid(), data.getOrderNum());
            if (backData == null) return R.fail("已在最后");
            // 和后一条记录的序号互换
            Integer backOrderNum = backData.getOrderNum();
            Integer curOrderNum = data.getOrderNum();
            // 序号互换
            cycleService.updateOrderNum(backData.getUid(), curOrderNum, headStaffUid, headStaffName);
            cycleService.updateOrderNum(uid, backOrderNum, headStaffUid, headStaffName);
        } else {
            return R.fail("类型错误");
        }

        // 清除门店服务周期缓存数据
        redisService.setNullLike(RedisParam.PROJECT_CYCLE);

        return R.ok();
    }

    @ApiOperation(value = "获取服务的周期")
    @PostMapping(value = "/getList")
    public R<Object> getList(
            @ApiParam(value = "员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "服务uid") @RequestParam(value = "projectUid") String projectUid,
            @ApiParam(value = "启用状态") @RequestParam(value = "enabled", required = false) Integer enabled
    ) {
        List<StoreProjectCycleDto> list = cycleService.listByProjectUidAndSourceAndEnabled(projectUid, 1, enabled);

        return R.ok(list);
    }

}
